#!/usr/bin/sh
#

# install mysql  sh -x install_mysql.sh
#wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
#定义5.7版本 别的版本不能该脚本 直接去官网下载https://dev.mysql.com/downloads/mysql/5.7.html  linux Generic 64
MYSQL_VERSION=mysql-5.7.20-linux-glibc2.12-x86_64
MYSQL_NEW_USER=biu
MYSQL_NEW_PASSWD=biu@2021
#定义上传解压目录
CUR_DIR=/opt

USER_DIR=/home/soft/
MYSQL_DIR=/home/soft/mysql
MYSQL_DATA_DIR=/home/soft/var/data/mysql
MYSQL_LOG_DIR=/home/soft/var/log/mysql
MYSQL_RUN_DIR=/home/soft/var/run/mysql



this_pid=$$
isContinue=$(ps aux | grep mysql | grep -v grep | grep -v ${this_pid} | wc -l)
if [ ${isContinue}  -eq 0 ]
then
    echo "Mysql is prepare tar ....."
else
    echo "Mysql is running ...."
    exit
fi


CreateDir()
{
    # 创建mysql用户 非登录状态
    groupadd mysql
    useradd -s /sbin/nologin -M -g mysql mysql

    #mkdir -p ${MYSQL_DIR}
    mkdir -p ${MYSQL_DATA_DIR}/
    mkdir -p ${MYSQL_LOG_DIR}/
    mkdir -p ${MYSQL_RUN_DIR}/

}

#下载好的tar.gz上传解压
#5.7的glibc2不用编译
Tar_Cp_MYSQL()
{
    mkdir -p ${USER_DIR}
	cd ${CUR_DIR}
    if [ -e ${MYSQL_VERSION}.tar.gz ]
    then
        cp  ${MYSQL_VERSION}.tar.gz ${USER_DIR}
        cd  ${USER_DIR}
        tar zxvf ${MYSQL_VERSION}.tar.gz
        mv ${MYSQL_VERSION} mysql
    else
        echo "${MYSQL_VERSION}.tar.gz file not exist ...."
        exit
    fi
}

ModifyMyCnf()
{
    #mysql默认会启动/etc/my.cnf 置换
    mv /etc/my.cnf /etc/my.cnf.bak
    #修改配置
	cat > /etc/my.cnf<<EOF
[client]
port = 8003
socket = ${MYSQL_RUN_DIR}/mysql.sock


[mysqld]
#如果主从复制,必设该值
server-id	= 166
port		= 8003

basedir = ${MYSQL_DIR}
datadir = ${MYSQL_DATA_DIR}
socket =  ${MYSQL_RUN_DIR}/mysql.sock
pid-file = ${MYSQL_RUN_DIR}/mysql.pid
log-error = ${MYSQL_LOG_DIR}/error.log

#mysql server不会对客户端连接使用反向dns解析
skip-name-resolve
skip-host-cache

#只对Myisam表起作用
#key_buffer_size = 32M
#服务端最大允许接收的数据包大小,默认是4M,一般情况下1M就可以
max_allowed_packet = 1M
#所有线程打开表的数目
table_open_cache = 512
#需要排序 会话 的缓存大小
sort_buffer_size = 768K
net_buffer_length = 8K
#对表的顺序扫描频繁
read_buffer_size = 1M
read_rnd_buffer_size = 512K
#不建议开启 失效太快
#query_cache_size = 8M
#临时表 group by 默认32M
tmp_table_size = 32M

innodb_buffer_pool_size = 128M

#最大连接数 如果服务器并连接请求两比较大 在调高,否则MySql会为每个连接提供连接缓冲区，产生大量的内存开销
max_connections = 1024


#慢sql
slow_query_log=true
log_queries_not_using_indexes=true
long_query_time=1

#bin log 关于增量备份
log-bin=mysql-bin
binlog_format=row

#异常重启  redo log 和 bin log
#(写性能 )默认是1  表示每次事务的 redo log 都直接持久化到磁盘  如果不是需要保留每一秒的事务 可以设置成2
innodb_flush_log_at_trx_commit = 1
#表示每次事务的 binlog 都持久化到磁盘
sync_binlog = 1

expire_logs_days = 30


default_password_lifetime = 0
EOF
}

StartMYSQL()
{
	chown -R mysql:mysql ${MYSQL_DIR}
	chown -R mysql:mysql ${MYSQL_DATA_DIR}
	chown -R mysql:mysql ${MYSQL_LOG_DIR}
	chown -R mysql:mysql ${MYSQL_RUN_DIR}

    echo "Initialize mysql ......"
    ${MYSQL_DIR}/bin/mysqld --initialize  --basedir=${MYSQL_DIR} --datadir=${MYSQL_DATA_DIR} --user=mysql

    echo "mysql 设置为服务......"
	cp ${MYSQL_DIR}/support-files/mysql.server /etc/init.d/mysql
	chmod 755 /etc/init.d/mysql
	chkconfig --add mysql
	/etc/init.d/mysql start
	echo "mysql is running..."

	passwd=$(grep -r 'A temporary password is generated for root@localhost:' ${MYSQL_LOG_DIR}/error.log)
	rootpasswd=${passwd#*A temporary password is generated for root@localhost:}
	echo $rootpasswd
}

UpdateRootPassword()
{
    echo "update root password password is ${rootpasswd}"
    cat >~/.emptymy.cnf<<EOF
[client]
user=root
password=${rootpasswd}
port=8003
socket = ${MYSQL_RUN_DIR}/mysql.sock
EOF
    newPassword="root"
    local UPDATE_ROOT_PASSWORD="alter user user() identified by '${newPassword}';"
    ${MYSQL_DIR}/bin/mysql --defaults-file=~/.emptymy.cnf  --connect-expired-password -e "$UPDATE_ROOT_PASSWORD"
    if [ $? -eq 0 ]
    then
        echo "Set root password Successfully."
    else
        echo "Set root password failed!"
        exit
    fi
    rm -f ~/.emptymy.cnf
}

CreateNormalUser()
{
    echo "add new user ...."
    cat >~/.empty.cnf<<EOF
[client]
user=root
password=${newPassword}
port=8003
socket = ${MYSQL_RUN_DIR}/mysql.sock
EOF

    local DELETE_NO_NEED_USER="delete from mysql.user where user != 'root';"
    ${MYSQL_DIR}/bin/mysql --defaults-file=~/.empty.cnf --connect-expired-password  -e "$DELETE_NO_NEED_USER"

    echo "new user is add ...."
    local ADD_NEW_USER="grant all privileges on *.* to '${MYSQL_NEW_USER}'@'%' identified by '${MYSQL_NEW_PASSWD}' with grant option;"
    ${MYSQL_DIR}/bin/mysql --defaults-file=~/.empty.cnf --connect-expired-password -e "$ADD_NEW_USER"

    ${MYSQL_DIR}/bin/mysql --defaults-file=~/.empty.cnf --connect-expired-password -e "flush privileges;"

    rm -f ~/.empty.cnf
    /etc/init.d/mysql restart

    echo "mysql restart ...."
}

AddEnvPath()
{
echo "mysql add env path ....start"
echo -e 'export PATH=$PATH:/home/soft/mysql/bin/'>>/etc/profile
source /etc/profile
echo "mysql add env path ....end" 
    
}
 
Tar_Cp_MYSQL
CreateDir
ModifyMyCnf
StartMYSQL
UpdateRootPassword
CreateNormalUser
AddEnvPath

echo 